<script setup lang="ts" >
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getRuleByIdAPI, updateRuleAPI } from '@/apis/car';
import type { Rule } from '@/types/car';
import { ElMessage } from 'element-plus';

const route = useRoute();
const router = useRouter();
const ruleId = route.params.id as string;
const rule = ref<Rule>({
  id: '',
  ruleNumber: '',
  ruleName: '',
  freeDuration: 0,
  chargeCeiling: 0,
  chargeType: 'duration',
  ruleNameView: ''
});

const getRuleById = async () => {
  try {
    const res = await getRuleByIdAPI(ruleId);
    rule.value = res.data;
  } catch (error) {
    ElMessage.error('获取规则失败');
  }
};

const saveRule = async () => {
  try {
    await updateRuleAPI(rule.value);
    ElMessage.success('保存成功');
    router.push({ name: 'CarRule' });
  } catch (error) {
    ElMessage.error('保存失败');
  }
};

const cancelEdit = () => {
  router.push({ name: 'CarRule' });
};

onMounted(() => {
  getRuleById();
});
</script>
<template>
  <div class="edit-rule-container">
    <h2>编辑计费规则</h2>
    <el-form :model="rule" label-width="120px">
      <el-form-item label="计费规则编号">
        <el-input v-model="rule.ruleNumber" />
      </el-form-item>
      <el-form-item label="计费规则名称">
        <el-input v-model="rule.ruleName" />
      </el-form-item>
      <el-form-item label="免费时长(分钟)">
        <el-input v-model.number="rule.freeDuration" />
      </el-form-item>
      <el-form-item label="收费上限(元)">
        <el-input v-model.number="rule.chargeCeiling" />
      </el-form-item>
      <el-form-item label="计费方式">
        <el-select v-model="rule.chargeType">
          <el-option label="时长收费" value="duration" />
          <el-option label="按次收费" value="turn" />
          <el-option label="分段收费" value="partition" />
        </el-select>
      </el-form-item>
      <el-form-item label="计费规则">
        <el-input v-model="rule.ruleNameView" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveRule">保存</el-button>
        <el-button @click="cancelEdit">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getRuleByIdAPI, updateRuleAPI } from '@/apis/car';
import type { Rule } from '@/types/car';
import { ElMessage } from 'element-plus';

const route = useRoute();
const router = useRouter();
const ruleId = route.params.id as string;
const rule = ref<Rule>({
  id: '',
  ruleNumber: '',
  ruleName: '',
  freeDuration: 0,
  chargeCeiling: 0,
  chargeType: 'duration',
  ruleNameView: ''
});

const getRuleById = async () => {
  try {
    const res = await getRuleByIdAPI(ruleId);
    rule.value = res.data;
  } catch (error) {
    ElMessage.error('获取规则失败');
  }
};

const saveRule = async () => {
  try {
    await updateRuleAPI(rule.value);
    ElMessage.success('保存成功');
    router.push({ name: 'CarRule' });
  } catch (error) {
    ElMessage.error('保存失败');
  }
};

const cancelEdit = () => {
  router.push({ name: 'CarRule' });
};

onMounted(() => {
  getRuleById();
});
</script>

<style lang="scss" scoped>
.edit-rule-container {
  padding: 20px;
  background-color: #fff;
}
</style>